/**
 * 
 */
package com.revenco.bi.dao.nutz;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Sqls;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Repository;

import com.revenco.bi.dao.TnChartParamsDao;
import com.revenco.bi.model.TnChartParams;
import com.revenco.bi.model.TnTableColumnRelation;
import com.revenco.bi.model.TnTableInfo;
import com.revenco.bi.model.TwUsrBaseInfoMo;

/**
 * @author ghq
 * @version $Revision:1.0.0, 2014年1月24日10:31:45
 */
@Repository
public class TnChartParamsDaoImpl extends AstractDaoImpl implements
		TnChartParamsDao {

	public TnChartParams getChartParamsById(String Id) {
		Condition condition = Cnd.where("paramsId", "=", Id);
		return (TnChartParams) this.getDao().query(TnChartParams.class,
				condition);
	}

	/**
	 * 保存筛选条件，参数表
	 */
	public void saveTnChartParams(TnChartParams tParams) {
		this.getDao().insert(tParams);

	}

	/**
	 * 更新筛选条件，参数表
	 */
	public void updateTnChartParams(TnChartParams tParams) {
		Chain c = Chain.from(tParams);
		Condition con = Cnd.where("chartId", "=", tParams.getChartId());
		con = Cnd.where("paramName", "=", tParams.getParamName());
		this.getDao().update(TnChartParams.class, c, con);
		// this.getDao().update(tParams);

	}

	/**
	 * 用名字和图表id查找记录
	 */
	public TnChartParams getChartParamsByIdName(String chartId, String name) {
		TnChartParams tnChartParams;
		Condition con = Cnd.where("CHART_ID", "=", chartId);
		con = Cnd.where("PARAM_CD", "=", name);
		tnChartParams = this.getDao().fetch(TnChartParams.class, con);
		return tnChartParams;

	}

	public List<TnChartParams> getChartParamsList(String chartId) {
		List<TnChartParams> tnChartParamsList;
		Condition condition = Cnd.where("CHART_ID", "=", chartId);
		tnChartParamsList = this.getDao().query(TnChartParams.class, condition);
		return tnChartParamsList;
	}

	public void deleteChartParams(TnChartParams tp) {
		this.getDao().delete(tp);
	}

	/**
	 * 根据图表id删除多个对象
	 * 
	 * @param paramId
	 * @return
	 */
	public boolean delChartParamsByIdName(String paramId) {
		boolean delFlag = false;
		List<TnChartParams> delChartParamsList = getChartParamsList(paramId);
		for (TnChartParams t : delChartParamsList) {
			this.getDao().delete(t);
		}
		delFlag = true;
		return delFlag;

	}

	public QueryResult getDefaultValue(String table, String column,Pager pager) {
		Condition cnd = Cnd.where("1", "=", 1);
		List<Record> list = this.getDao().query(table, cnd,pager);
		pager.setRecordCount(this.getDao().count(table));
        return new QueryResult(list, pager);
	}
	
	public void deleteByChartId(String chartId) {
		Sql sql = Sqls
				.create("Delete FROM FRNT.TN_CHART_PARAMS WHERE CHART_ID = @chartId");
		sql.params().set("chartId", chartId);
		this.getDao().execute(sql);
	}
	public QueryResult getPetList(Dao dao, int pageNumber, int pageSize){
        Pager pager = this.getDao().createPager(pageNumber, pageSize);
        List<TwUsrBaseInfoMo> list = this.getDao().query(TwUsrBaseInfoMo.class, null, pager);
        pager.setRecordCount(dao.count(TwUsrBaseInfoMo.class));
        return new QueryResult(list, pager);
	}

	public void saveTnTableColumnRelation(TnTableColumnRelation table) {
		this.getDao().insert(table);
	}
	
}
